//
//  LYWebViewController.m
//  artist
//
//  Created by Jay_Apple on 16/5/13.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYWebViewController.h"
#import "WebViewJavascriptBridge.h"

#define classMainUrl  @"http://esj.zzzedu.cn/webview/view/course/course_details.php?"
#define guideMainUrl @"http://esj.zzzedu.cn/webview/view/guide/index.php?"
#define videoMainUrl @"http://esj.zzzedu.cn/webview/view/video/video_details.php?"
#define stuMainUrl @"http://esj.zzzedu.cn/webview/view/student/index.php?"
#define teaMainUrl @"http://esj.zzzedu.cn/webview/view/teacher/teacher.php"
#define orzMainUrl @"http://esj.zzzedu.cn/webview/view/org/org.php?"
#define articalMainUrl @"http://esj.zzzedu.cn/webview/view/article/index.html?id="
#define testMainUrl @"http://esj.zzzedu.cn/UserApi/Testing/solution?id="

@interface LYWebViewController ()<UIWebViewDelegate>

@property (nonatomic, copy) NSString *ID;

@property (nonatomic, copy) NSString *urlStr;

@property (nonatomic, weak) UIWebView *webView;

@property (nonatomic, weak) UIView *bottomBar;

@property (nonatomic, assign) WebType type;

@property WebViewJavascriptBridge* bridge;

//btns
/** 申请好友 */
@property (nonatomic, weak) UIButton *applyBtn;
/** 收藏 */
@property (nonatomic, weak) UIButton *collectBtn;
/** 在线咨询 */
@property (nonatomic, weak) UIButton *consultBtn;
/** 电话咨询 */
@property (nonatomic, weak) UIButton *callBtn;
/** 下载 */
@property (nonatomic, weak) UIButton *downLoadBtn;
/** 关注 */
@property (nonatomic, weak) UIButton *followBtn;
/** 评论 */
@property (nonatomic, weak) UIButton *commentBtn;



@end

@implementation LYWebViewController

#pragma mark - 构造方法
+ (instancetype)webVCWithID:(NSString *) ID andType:(WebType)type{

    LYWebViewController *webVC = [[self alloc] init];
    webVC.ID = ID;
    webVC.type = type;
    return webVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化webView
    [self setUpWebView];
    
    //桥接JS
    [self buildBridge];

}




#pragma mark - 重写
- (void)setType:(WebType)type{

    _type = type;
    
    NSString *parameter = [NSString stringWithFormat:@"course_id=%@&token=%@",self.ID,LYToken];
    
    switch (_type) {
        case WebTypeArtical:
            self.urlStr = [articalMainUrl stringByAppendingString:self.ID];
            self.navigationItem.title = @"文章";
            break;
        
    case WebTypeTestExplain:
            self.urlStr = [testMainUrl stringByAppendingString:self.ID];
            self.navigationItem.title = @"题解";
            break;
            
        default:
            break;
    }
    
    

}

#pragma mark - 懒加载
- (UIWebView *)webView{

    if (_webView == nil) {
        UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
        webView.delegate = self;
        [self.view addSubview:webView];
        _webView = webView;
    }
    return _webView;

}

- (UIView *)bottomBar{

    if (_bottomBar == nil) {
        UIView *bottomBar = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.My_Height - 60, self.view.My_Width, 60)];
        [self.view insertSubview:bottomBar aboveSubview:self.webView];
        _bottomBar = bottomBar;
    }
    
    return _bottomBar;
}

- (UIButton *)applyBtn{

    if (_applyBtn == nil) {
        UIButton *applyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [applyBtn addTarget:self action:@selector(applyBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.bottomBar addSubview:applyBtn];
        _applyBtn = applyBtn;
    }
    
    return _applyBtn;

}

- (UIButton *)collectBtn{

    
    if (_collectBtn == nil) {
        UIButton *collectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [collectBtn addTarget:self action:@selector(collectBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.bottomBar addSubview:collectBtn];
        _collectBtn = collectBtn;
    }
    
    return _collectBtn;
}

- (UIButton *)followBtn{
    
    if (_followBtn == nil) {
        UIButton *followBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [followBtn addTarget:self action:@selector(followBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.bottomBar addSubview:followBtn];
        _followBtn = followBtn;
    }
    
    return _followBtn;

}

- (UIButton *)commentBtn{

    if (_commentBtn == nil) {
        UIButton *commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        
        [commentBtn addTarget:self action:@selector(commentBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.bottomBar addSubview:commentBtn];
        _commentBtn = commentBtn;
    }
    
    return _commentBtn;


}

- (UIButton *)consultBtn{

    if (_consultBtn == nil) {
        UIButton *consultBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [consultBtn addTarget:self action:@selector(consultBtnBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.bottomBar addSubview:consultBtn];
        _consultBtn = consultBtn;
    }
    
    return _consultBtn;


}

- (UIButton *)callBtn{

    if (_callBtn == nil) {
        UIButton *callBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [callBtn addTarget:self action:@selector(callBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.bottomBar addSubview:callBtn];
        _callBtn = callBtn;
    }
    
    return _callBtn;
}

- (UIButton *)downLoadBtn{

    if (_downLoadBtn == nil) {
        UIButton *downLoadBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [downLoadBtn addTarget:self action:@selector(downLoadBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.bottomBar addSubview:downLoadBtn];
        _downLoadBtn = downLoadBtn;
    }
    
    return _downLoadBtn;


}

#pragma mark - 按钮点击事件
/**
 *  关注
 */
- (void)followBtnOnclick:(UIButton *)btn{



}

/**
 *  在线咨询
 */
- (void)consultBtnBtnOnclick:(UIButton *)btn{



}

/**
 *  电话咨询
 */
- (void)callBtnOnclick:(UIButton *)btn{

    

}

/**
 *  收藏
 */
- (void)collectBtnOnclick:(UIButton *)btn{
    
    
    
}

/**
 *  评论
 */
- (void)commentBtnOnclick:(UIButton *)btn{
    
    
    
}

/**
 *  下载
 */
- (void)downLoadBtnOnclick:(UIButton *)btn{
    
    
    
}

/**
 *  申请好友
 */
- (void)applyBtnOnclick:(UIButton *)btn{



}


#pragma mark - 网络请求
/**
 *  关注
 */

/**
 *  收藏
 */

/**
 *  下载
 */

/**
 *  申请好友
 */



#pragma mark - 初始化
/**
 *  初始化webView
 */
- (void)setUpWebView{
    
    //自适应屏幕
    self.webView.scalesPageToFit = YES;
    
    //加载请求
    if(self.urlStr){
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0];
        
        [self.webView loadRequest:request];
    }
}


/**
 *  桥接JS
 */
- (void)buildBridge{
    
    if (_bridge) { return; }
    
    __weak typeof(self) weakSelf = self;
    
    [WebViewJavascriptBridge enableLogging];
    
    _bridge = [WebViewJavascriptBridge bridgeForWebView:_webView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) {
        
        NSLog(@"receivedMessage");
        
    }];
    
    //支付桥接
    [_bridge registerHandler:@"buyRightNow" handler:^(id data, WVJBResponseCallback responseCallback) {
        
        
        
    }];
    
    
    
    //登陆状态桥接
    [_bridge registerHandler:@"getLoginMessage" handler:^(id data, WVJBResponseCallback responseCallback) {
        
    
    }];
    
    //HUD弹窗桥接
    [_bridge registerHandler:@"toast" handler:^(id data, WVJBResponseCallback responseCallback) {
        
        [MBProgressHUD showError:data toView:weakSelf.webView];
    }];
    
}


#pragma mark - webView代理

//- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
//    
//    NSString *urlStr = request.URL.absoluteString;
//    
//    if ([urlStr isEqualToString:self.urlStr]) {
//        
//        return YES;
//        
//    }else{//不是重新加载
//        
//        UIViewController *vc = [VVYChooseWebVCTool chooseWebVCWithUrlStr:urlStr];
//        [self.navigationController pushViewController:vc animated:YES];
//        return NO;
//        
//    }
//}

- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    [MBProgressHUD showMessage:nil toView:webView];
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
    [MBProgressHUD hideHUDForView:webView];
    
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
    
    [MBProgressHUD hideHUDForView:webView];
    [MBProgressHUD showError:@"网络不给力" toView:webView];
}

@end
